#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QFrame>
#include <QtGui/QComboBox>
#include <QtGui/QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include<QtCore/QVariant>
#include <QtDebug>

#include "setpassworddialog.h"

SetPasswordDialog::SetPasswordDialog(QWidget *parent) :
    QDialog(parent)
{
    createItems();
    setWindowTitle(tr("密码设置"));
    this->setFixedSize (QSize(400, 340));
}
SetPasswordDialog::~SetPasswordDialog()
{

}
void SetPasswordDialog::createItems()
{

    passwordFrame = new QFrame(this);
    passwordFrame->setGeometry(QRect(0, 5 ,400, 290));
    passwordFrame->setFrameStyle(QFrame::Box);

    passwordTitleSetLabel = new QLabel(tr("密码设置"), this);
    passwordTitleSetLabel->setGeometry(QRect(160, 15 , 80, 24));
    passwordTitleSetLabel->setFrameShape(QFrame::Box);
    passwordTitleSetLabel->setAlignment(Qt::AlignCenter);

    hostNumLabel = new QLabel(tr("本机号码:"), this);
    hostNumLabel->setGeometry(QRect(70, 60 , 80, 24));
    passwordResetLabel = new QLabel(tr("复位密码:"), this);
    passwordResetLabel->setGeometry(QRect(70, 100 , 80, 24));
    passwordOkResetLabel = new QLabel(tr("复位密码确定:"), this);
    passwordOkResetLabel->setGeometry(QRect(70, 140 , 100, 24));
    passwordSetLabel = new QLabel(tr("设置密码:"),this);
    passwordSetLabel->setGeometry(QRect(70, 180 , 80, 24));
    passwordOkSetLabel = new QLabel(tr("设置密码确定:"),this);
    passwordOkSetLabel->setGeometry(QRect(70, 220 , 100, 24));

    hostNumCombox  = new QComboBox(this);
    hostNumCombox->setGeometry(QRect(190, 60 , 80, 24));


    resetLineEdit = new QLineEdit(this);
    resetLineEdit->setGeometry(QRect(190, 100 , 120, 24));
    resetLineEdit->setEchoMode(QLineEdit::Password);
    resetOkLineEdit = new QLineEdit(this);
    resetOkLineEdit->setGeometry(QRect(190, 140 , 120, 24));
    resetOkLineEdit->setEchoMode(QLineEdit::Password);
    setLineEdit = new QLineEdit(this);
    setLineEdit->setGeometry(QRect(190, 180 , 120, 24));
    setLineEdit->setEchoMode(QLineEdit::Password);
    setOkLineEdit = new QLineEdit(this);
    setOkLineEdit->setGeometry(QRect(190, 220 , 120, 24));
    setOkLineEdit->setEchoMode(QLineEdit::Password);



    QSqlDatabase::database().transaction();
    QSqlQuery hostquery;
    hostquery.setForwardOnly(true);
    hostquery.exec("select hostnum from hostset");
    int hostnum = 1;
    while (hostquery.next())
    {
        hostnum = hostquery.value(0).toInt();
        hostNumCombox->addItem(tr("%1").arg(hostquery.value(0).toInt()));
    }
    QSqlDatabase::database().commit();


    for(int i = 1; i <= 64; i++)
    {
        if(i != hostnum)
            hostNumCombox->addItem(tr("%1").arg(i));
    }

    okButton = new QPushButton(tr("确定"), this);
    okButton->setGeometry(QRect(70, 310, 80, 24));
    cancelButton = new QPushButton(tr("取消"), this);
    cancelButton->setGeometry(QRect(220, 310, 80, 24));
    cancelButton->setFocus();//set default button

    QSqlDatabase::database().transaction();
    QSqlQuery query;
    query.setForwardOnly(true);
    query.exec("select * from password");
    while (query.next())
    {
        switch(query.value(0).toInt())
        {
        case 1:
        {
            resetLineEdit->setText(query.value(1).toString());
            resetOkLineEdit->setText(query.value(1).toString());
        }
        break;
        case 2:
        {
            setLineEdit->setText(query.value(1).toString());
            setOkLineEdit->setText(query.value(1).toString());
        }
        break;
        }
    }
    QSqlDatabase::database().commit();

    connect(okButton, SIGNAL(clicked()), this, SLOT(okSaveSlot()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(this->parentWidget(), SIGNAL(closedialog()), this, SLOT(close()));

}
void SetPasswordDialog::okSaveSlot()
{
    if ((resetLineEdit->text() != resetOkLineEdit->text()) || (setLineEdit->text() != setOkLineEdit->text()))
    {
        QMessageBox *box = new QMessageBox(tr("Warning"),
                                           tr("两次输入的密码不匹配!"),
                                           QMessageBox::Warning,
                                           QMessageBox::Ok,
                                           QMessageBox::NoButton,
                                           QMessageBox::NoButton,
                                           this);
        box->setAttribute(Qt::WA_DeleteOnClose);
        connect(this->parentWidget(), SIGNAL(closedialog()), box, SLOT(close()));
        box->exec();
    }
    else
    {
        QMessageBox *box1 = new QMessageBox(tr("Question"),
                                            tr("确定保存修改？"),
                                            QMessageBox::Question,
                                            QMessageBox::Yes | QMessageBox::Default,
                                            QMessageBox::NoButton,
                                            QMessageBox::Cancel | QMessageBox::Escape,
                                            this);
        box1->setAttribute(Qt::WA_DeleteOnClose);
        connect(this->parentWidget(), SIGNAL(closedialog()), box1, SLOT(close()));
        switch(box1->exec())
        {
        case QMessageBox::Yes:
        {
            // save and exit
            QSqlDatabase::database().transaction();
            QSqlQuery query;
            query.clear();
            for (int i = 1;i <= 2; i ++)
            {
                query.prepare(QString("UPDATE password SET "
                                      "passwordinfo=?"
                                      "WHERE id = %1").arg(i));
                switch(i)
                {
                case 1:
                    query.addBindValue(resetOkLineEdit->text());
                    break;
                case 2:
                    query.addBindValue(setOkLineEdit->text());
                    break;
                }
                bool bSuccess = query.exec();
                if (!bSuccess)
                {
                    QSqlError lastError = query.lastError();
                    qDebug() << lastError.driverText()
                             << QString(QObject::tr("password update error"));
                }
            }
            QSqlDatabase::database().commit();

            QSqlDatabase::database().transaction();
            QSqlQuery hostquery;
            hostquery.clear();
            hostquery.prepare("UPDATE hostset SET "
                              "hostnum = ?"
                              "WHERE id = 1");

            hostquery.addBindValue(hostNumCombox->currentText().toInt());


            bool bSuccess = hostquery.exec();
            if (!bSuccess)
            {
                QSqlError lastError = hostquery.lastError();
                qDebug() << lastError.driverText()
                         << QString(QObject::tr("hostset update error"));
            }


            QSqlDatabase::database().commit();

            this->close();
        }
        break;
        case QMessageBox::Cancel:
        {
            // don't save and exit
            this->close();
        }
        break;
        }
    }
}
